//
//  DatebaseUtil.m
//  LOU
//
//  Created by iMacOne on 15/11/11.
//  Copyright © 2015年 UNOHACHA. All rights reserved.
//

#import "DatebaseUtil.h"
#import "ToolUtil.h"
#import "JdbcUtil.h"
#import "DateUtil.h"
#import "FileUtil.h"
@implementation DatebaseUtil
#pragma mark 写入设置
+ (void)write:(NSString*)key value:(NSString*)value
{
    value = ([ToolUtil isBlank:value] ? @"" : value);
    NSString *sql = @"select count(*) from setting where key = ?";
    if ([JdbcUtil validate:sql args:@[key]]) {
        sql = @"update setting set value = ? where key = ?";
        [JdbcUtil manage:sql args:@[value, key]];
    } else {
        sql = @"insert into setting (key, value) values (?, ?)";
        [JdbcUtil manage:sql args:@[key, value]];
    }

    
}

#pragma mark 获得设置
+ (NSString*)getSetting:(NSString*)key
{
    NSString *sql = @"select value from setting where key = ?";
    return [JdbcUtil getValue:sql args:@[key]];

}

#pragma mark 添加备份数据
+ (void)add:(NSString*)url param:(NSString*)param data:(NSString*)data
{
    param = ([ToolUtil isBlank:param] ? @"" : param);
    if (url.length >= 200 || param.length >= 500 || data.length >= 50000) {
        return;
    }
    NSMutableDictionary *map = [DatebaseUtil getMap:url param:param];
    if (nil != map) {
        [JdbcUtil delById:@"backup" _id:[map[@"id"] intValue]];
    }
    NSString *memberId ;
//    [NSString stringWithFormat:@"%d", (nil == [Constant bean].loginer ? 0 : [Constant bean].loginer._id)];
    NSString *sql = @"insert into backup (url, param, memberId, data, time) values (?, ?, ?, ?, ?)";
    NSArray *args = @[url, param, memberId, data, [DateUtil formatDateTimeStr:[NSDate date]]];
    [JdbcUtil manage:sql args:args];
}

#pragma mark 获得备份数据map
+ (NSMutableDictionary*)getMap:(NSString*)url param:(NSString*)param
{
    param = ([ToolUtil isBlank:param] ? @"" : param);
    NSString *memberId ;
//    [NSString stringWithFormat:@"%d", (nil == [Constant bean].loginer ? 0 : [Constant bean].loginer._id)];
    NSString *sql = @"select id, data, time from backup where url = ? and param = ? and memberId = ?";
    return [JdbcUtil getMap:sql args:@[url, param, memberId] keys:@[@"id", @"data", @"time"]];
}

#pragma mark 保存图片
+ (void)addImg:(NSString*)url path:(NSString*)path
{
    NSString *time = [DateUtil formatDateTimeStr:[NSDate date]];
    NSMutableDictionary *map = [JdbcUtil getMap:@"select id, path from image where url = ?" args:@[url] keys:@[@"id", @"path"]];
    if (nil != map) {
        [FileUtil delFile:map[@"path"]];
        [JdbcUtil manage:@"update image set path = ?, editTime = ? where id = ?" args:@[path, time, map[@"id"]]];
    } else {
        NSString *sql = @"insert into image (url, path, addTime, editTime) values (?, ?, ?, ?)";
        [JdbcUtil manage:sql args:@[url, path, time, time]];
    }

}

#pragma mark 获取图片的map, 在数据库中查找
+ (NSMutableDictionary*)getImgMap:(NSString*)url
{
    NSString *sql = @"select id, path from image where url = ?";
    NSMutableDictionary *map = [JdbcUtil getMap:sql args:@[url] keys:@[@"id", @"path"]];
    if (nil != map) {
        sql = @"update image set editTime = ? where id = ?";
        NSString *time = [DateUtil formatDateTimeStr:[NSDate date]];
        [JdbcUtil manage:sql args:@[time, map[@"id"]]];
    }
    return map;
}
@end
